package com.sakila.services;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
//import static java.util.Collections.*;
//import static java.lang.Math.*;
import com.sakila.dao.ActorDAO;
import com.sakila.dao.IActorDAO;
import com.sakila.dao.FilmActorDAO;
import com.sakila.dbo.ActorDBO;
import com.sakila.dbo.FilmActorDBO;

public class FilmActorServiceImpl implements FilmActorService {

	List<Map<Integer, List<Integer>>> finalist = new ArrayList<Map<Integer, List<Integer>>>();
	List<Map<Integer, List<ActorDBO>>> finalistdbo = new ArrayList<Map<Integer, List<ActorDBO>>>();
	
	Map<Integer, List<Integer>> finaMap = new HashMap<Integer, List<Integer>>();
	Map<Integer, List<ActorDBO>> finaMapdbo = new HashMap<Integer, List<ActorDBO>>();
	List<ActorDBO> list = new ArrayList<ActorDBO>();
	List<Integer> fid = new ArrayList<Integer>();
	List<ActorDBO> listdbo = new ArrayList<ActorDBO>();
	FilmActorDBO filmactor;
	int count = 0;
	IActorDAO dao = new ActorDAO();
	ActorDBO dbo ;

	@Override
	public List<Map<Integer, List<ActorDBO>>> getActorsByFilm() {
		List<FilmActorDBO> Flist = new FilmActorDAO().findAll();
		Iterator<FilmActorDBO> it = Flist.iterator();
		while (it.hasNext()) {

			filmactor = it.next();
			int key = filmactor.getFilm_id();
			if (finaMapdbo.containsKey(key)) {
				//fid = finaMap.get(key);
				listdbo = finaMapdbo.get(key);
			} else {
				//fid = new ArrayList<Integer>();
				listdbo = new ArrayList<ActorDBO>();
				count++;
			}

			Integer ActorId = filmactor.getActor_id();
			dbo = dao.findById(ActorId);
			listdbo.add(dbo);
			//fid.add(filmId);
			finaMapdbo.put(key, listdbo);
			//finaMap.put(key, fid);

		}
		
		System.out.println(count);
		
		//finalist.add(finaMap);
		finalistdbo.add(finaMapdbo);

		// return finalist.subList(0, min(5, list.size()));
		//return finalist;
		return finalistdbo;

	}

}
